﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_a_advert : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }   
    protected void lbtAddNewItem_Click(object sender, EventArgs e)
    {
        string strFileName = FileUploadAdvert.FileName;
        if (!string.IsNullOrEmpty(strFileName))
        {
            try
            {
                Stream fStream = FileUploadAdvert.FileContent;
                strFileName = FileUploadAdvert.FileName.Replace(' ', '_');
                strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
                strFileName = CommonBLL.GetFilename(strFileName);

                string filepathsmallimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "smallimages//") + strFileName;
                string filepathlargeimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "images//") + strFileName;

                // Validate the uploaded file
                // 1. The uploaded file should not be a blank file
                if (FileUploadAdvert.FileBytes.Length == 0)
                {
                    lblMessage.Text = "File bạn chọn bị lỗi! dung lượng file = " + FileUploadAdvert.FileBytes.Length.ToString() + " bytes";
                    return;
                }
                // 2. The uploaded file should be having one of the allowed extensions
                string ext = System.IO.Path.GetExtension(FileUploadAdvert.FileName).TrimStart(".".ToCharArray()).ToLower();
                if ((ext != "jpeg") && (ext != "jpg") && (ext != "png") && (ext != "gif") && (ext != "bmp"))
                {
                    lblMessage.Text = "Định dạng file bạn chọn không hợp lệ (File hợp lệ dạng file ảnh)";
                    return;
                }

                // Validation successful
                // Load the image into Bitmap Object

                Bitmap uploadedImage = new Bitmap(FileUploadAdvert.FileContent);

                // Set the maximum width and height here.
                // You can make this versatile by getting these values from
                // QueryString or textboxes
                int maxWidth = 170;
                int maxHeight = 50;

                // Resize the image
                Bitmap resizedImage = CommonBLL.GetScaledPicture(uploadedImage, maxWidth, maxHeight);

                //Save the image
                // String virtualPath = "~/temp/" + System.Guid.NewGuid().ToString() + "." + ext;
                //String tempFileName = Server.MapPath(virtualPath);
                resizedImage.Save(filepathsmallimage, uploadedImage.RawFormat);
                FileUploadAdvert.SaveAs(filepathlargeimage);
                new CmsBLL().Adverts_InsertItem(strFileName, txtUrl.Text + "", txtDescription.Text + "", int.Parse("0" + rdTarget.SelectedValue), int.Parse("0" + DropDownListLocation.SelectedValue), int.Parse("0" + txtSortIndex.Text), int.Parse("0" + rdActive.SelectedValue), DateTime.Now, DateTime.Now);
                lblMessage.Text = "Thêm mới thành công!";
                ResetForm();
                // Load the resized image on the browser
                //picture.ImageUrl = virtualPath;
                //imageContainer.Visible = true;
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Eror! Message: " + ex.Message;
            }

        }
        else
        {
            lblMessage.Text = "Bạn chưa chọn file!";
            return;
        }        
    }
    private void ResetForm()
    {
        txtUrl.Text = "";
        txtDescription.Text = "";
        rdTarget.SelectedValue = "0";
        DropDownListLocation.SelectedValue = "1";
        txtSortIndex.Text = "";    
        rdActive.SelectedValue = "1";
        txtUrl.Focus();
    }
}